import { defineConfig } from 'vite'
import react from '@vitejs/plugin-react'
import path from 'path'
import tailwindcss from '@tailwindcss/vite'
import ys from 'vite-plugin-compression'

// https://vite.dev/config/
export default defineConfig({
  plugins: [react(), tailwindcss(),
  ys({
    // 12.vite打包构建 配置gzip代码压缩
    algorithm: 'gzip'
  }), {
    // 11.自定义vite插件my-plugin将构建后的代码中所有的console.log替换为console.info
    name: "my-plugin", transform(code) {
      return code.replace(/console\.log/g, 'console.info')
    }
  }
  ],
  resolve: {
    alias: {
      '@': path.resolve(__dirname, 'src')
    }
  },
  // 10.vite构建时生产环境时移除console
  esbuild: {
    drop: ['console', 'debugger']
  },
  server: {
    proxy: {
      '^/api': {
        target: 'http://172.27.2.110',
        changeOrigin: true,
        rewrite: (path) => path.replace(/^\/api/, ''),
      },
    }
  },
  // 9.做vite构建的打包配置，静态文件按css,js,image三种类型分类。打包后，文件放到对应三个文件夹内，做配置.
  build:{
    rollupOptions:{
      output:{
        assetFileNames:'[ext]-[name]-[has].[ext]',
        sanitizeFileName:'js/[ext]-[hash].js'
      }
    }
  }
})


